package digui;

import java.util.List;

/**
 * create by mwh 2021-12-31
 **/

public class Solution {
    public static ListNode  reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    //迭代
    public static ListNode  diedai(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while(cur!=null){
          ListNode next = cur.next;
          cur.next = pre;
          //向后移动
            pre = cur;
            cur = next;
        }
        return pre;
    }



    public static void main(String[] args) {

        ListNode listNode3 = new ListNode(3);
        ListNode listNode2 = new ListNode(2,listNode3);
        ListNode listNode1 = new ListNode(1,listNode2);

        System.out.println(listNode1);
//        System.out.println(diedai(listNode1));
        System.out.println(reverseList(listNode1));
    }

}
